package com.greatwqs.wmall.common.provider.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;

/**
 * DataSourceUtil
 *
 * @author wang-qingsong
 */
public class DataSourceUtil {

    /**
     * @see DruidDataSource
     */
    public static DruidDataSource getDruid(final String url, final String username, final String password, final String driverClassName, final int conInitialSize, final int conMaxActive) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);
        dataSource.setInitialSize(conInitialSize);
        dataSource.setMaxActive(conMaxActive);
        dataSource.setValidationQuery("select 1");
        // test connection
        dataSource.setTestOnBorrow(true);
        dataSource.setTestWhileIdle(true);
        dataSource.setTestOnReturn(false);
        return dataSource;
    }

    /**
     * @see HikariDataSource
     */
    public static DataSource getHikari(final String url, final String username, final String password, final String driverClassName, final int conInitialSize, final int conMaxActive) {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);
        dataSource.setMinimumIdle(conInitialSize);
        dataSource.setMaximumPoolSize(conMaxActive);
        dataSource.setConnectionTestQuery("select 1");
        return dataSource;
    }
}
